<?php

namespace Admin\Controller;


use Common\Controller\BaseController;
use Common\Model\DetailModel;
use Common\Model\GoodsModel;

class GoodsController extends BaseController
{
    protected $db;
    public function __init(){

        $this->db=new GoodsModel();

    }


    public function index(){

//       $data= $this->db->select();
////       dd($data);
//        $this->assign('data',$data);




        $goods = M('Goods'); // 实例化User对象
        $count      = $goods->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //dump($Page);
        $show       = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $goods->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('data',$list);// 赋值数据集
        //给追加样式
        $show .= <<<str
                <style>
                    /*其他页、上一页、下一页样式*/
                    .pagination .num,.prev,.next{
                        position: relative;
                        float: left;
                        padding: 6px 12px;
                        margin-left: -1px;
                        line-height: 1.42857143;
                        color: #337ab7;
                        text-decoration: none;
                        background-color: #fff;
                        border: 1px solid #ddd
                    }
                    /*当前分页样式*/
                    .pagination .current{
                        position: relative;
                        float: left;
                        padding: 6px 12px;
                        margin-left: -1px;
                        line-height: 1.42857143;
                        color: red;
                        text-decoration: none;
                        background-color: #fff;
                        border: 1px solid #ddd
                    }
                    .pagination .headerPage{
                        position: relative;
                        float: left;
                        padding: 6px 12px;
                        text-decoration: none;
                        background-color: #fff;

                    }
                </style>

str;

        $this->assign('page',$show);// 赋值分页输出
        //显示模板文件
//        $this->display();


        $this->display();
    }

    public function store(){
            if (IS_POST){
//                dd($_POST);die;
                $res = $this->db->store(I('post.'));
                if($res['valid']){
                    //说明成功
                    $this->success($res['msg'],u('index'));exit;
                }else{
                    //说明添加失败
                    $this->error($res['msg']);exit;
                }

            }


        $cateData = $this->db->getCate();
        $this->assign('cateData',$cateData);
//        dd($cateData);
        //2.获取品牌
        $brandData=$this->db->getBrand();
        $this->assign('brandData',$brandData);

        $this->display();
}
    public function ajaxGetSpec()
    {
        if(IS_AJAX)
        {
            //1.接受tid
            $tid = I('post.tid');
//            dd($tid);
            //2.根据tid在类型属性表中获取数据
            $data = m('typeattribute')->where("tid={$tid}")->select();
            //3.将数据中每一条的属性值【tavalue】变为数组
            foreach ($data as $k=>$v){
                $data[$k]['tvalue'] = explode(',',$v['tvalue']);
            }
//            dd($data);
            //4.将数据返回
            $this->ajaxReturn($data);
        }
    }



    public function edit(){

        $gid=I('get.gid');
//        dd($gid);die;
        if (IS_POST){
            $res = $this->db->store(i('post.'),$gid);
            if($res['valid']){
                //说明成功
                $this->success($res['msg'],u('index'));exit;
            }else{
                //说明添加失败
                $this->error($res['msg']);exit;
            }
        }






//        dd($gid);
        $data= $this->db->find($gid);
         $this->assign('data',$data);
//      dd($data);

        $cateData = $this->db->getCate();
//        dd($cateData);
        $this->assign('cateData',$cateData);
//        dd($cateData);

        //2.获取品牌
        $brandData=$this->db->getBrand();
        $this->assign('brandData',$brandData);
//        dd($brandData);

        //3.获取商品详情表
//        where("gid={$gid}")->select()
        $detailData=(new DetailModel())->where("gid={$gid}")->find();
//        dd($gid);
//        dd($detailData);
        $this->assign('detailData',$detailData);
        //商品属性
//        $Model = M('User');typeattribute
//        $Model->alias('a')->join('__DEPT__ b ON b.user_id= a.id')->select();
        $goodsAttr=m('goods')->alias('g')
            ->join('__DETAIL__ h ON h.gid = g.gid')->where("g.gid={$gid}" )
            ->find();

        $this->assign('oldData',$goodsAttr);
//        dd($goodsAttr);

  //找typeattribute属性数据
        $attrData=m('typeattribute')->where(['tid'=>$goodsAttr['tid'],'ttype'=>1])->select();
        //将数据中每一条的属性值【tavalue】变为数组
        foreach ($attrData as $k=>$v){
            $attrData[$k]['tvalue']=explode(',',$v['tvalue']);
        }
   //god_attr
//        dd($attrData);

        //获取商品属性表中获取当前商品所有规则属性
        $hasAttr=m('goods_attr')->where(['gid'=>$gid])->getField('gtvalue',true);
//        dd($hasAttr);
        $this->assign('attrData',$attrData);
        $this->assign('hasAttr',$hasAttr);
//获取规格数据
        $specData = m('typeattribute')->alias('ta')
            ->join("__GOODS_ATTR__ ga on ta.taid=ga.taid")
            ->where("ta.ttype=2 and ga.gid=$gid")->select();

        foreach ($specData as $k=>$v){
            $specData[$k]['tvalue']=explode(',',$v['tvalue']);
        }
//            dd($specData);
        $this->assign('specData',$specData);





        $this->display();
    }


    public function del(){

        $gid=i('get.gid');
        $res =$this->db->del( $gid);

        if($res['valid']){
            //说明成功
        $this->success($res['msg'],u('index'));exit;
    }else{
            //说明添加失败
            $this->error($res['msg']);exit;
        }
    }

}